package com.sdx.console.crm.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.util.DateUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.crm.entity.CrmRepresentiveConfig;
import com.sdx.console.crm.service.CrmRepresentiveConfigService;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 客服设置 接口
 */
@RequestMapping("/crm/crmRepresentiveConfig")
@Controller
public class CrmRepresentiveConfigController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(CrmRepresentiveConfigController.class);
    private static final String MODEL = "/crm/crmRepresentiveConfig";


    @Resource
	private CrmRepresentiveConfigService crmRepresentiveConfigService;

	/**
	 * 初始化页面需要的列表数据，
	 *
	 */
	private void setPageParam(ModelAndView view,HttpServletRequest request) {

	}

    /* 页面 */

    @GetMapping("/index.do")
    @Function("crm.crmRepresentiveConfig.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
		setPageParam(view,request);
        return view;
    }

    @GetMapping("/edit.do")
    @Function("crm.crmRepresentiveConfig.edit")
    @ResponseBody
    public ModelAndView edit(HttpServletRequest request,Integer configId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        CrmRepresentiveConfig crmRepresentiveConfig = crmRepresentiveConfigService.queryById(configId);
        view.addObject("crmRepresentiveConfig", crmRepresentiveConfig);
		setPageParam(view,request);
		try {
			view.addObject("crmRepresentiveConfigJson", JacksonUtils.toJsonString(crmRepresentiveConfig,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("crm.crmRepresentiveConfig.add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
		setPageParam(view,request);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("crm.crmRepresentiveConfig.query")
    @ResponseBody
    public JsonResult<PageQuery> list(CrmRepresentiveConfig condtion)
    {
        PageQuery page = condtion.getPageQuery();
        crmRepresentiveConfigService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,CrmRepresentiveConfig crmRepresentiveConfig,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		String bucketName = "sdxcomm";
		CoreUser user = getLoginUser(request);

    	if (isNew) {

			crmRepresentiveConfigService.save(crmRepresentiveConfig);
		} else {

			crmRepresentiveConfigService.updateTemplate(crmRepresentiveConfig);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("crm.crmRepresentiveConfig.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)CrmRepresentiveConfig crmRepresentiveConfig,HttpServletRequest request
		)
    {

        return save(true,crmRepresentiveConfig,request
		);
    }

    @PostMapping("/update.json")
    @Function("crm.crmRepresentiveConfig.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  CrmRepresentiveConfig crmRepresentiveConfig,HttpServletRequest request
		) {
		return save(false,crmRepresentiveConfig,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("crm.crmRepresentiveConfig.query")
    @ResponseBody
    public JsonResult<CrmRepresentiveConfig>queryInfo(Integer configId) {
        CrmRepresentiveConfig crmRepresentiveConfig = crmRepresentiveConfigService.queryById( configId);
        return  JsonResult.success(crmRepresentiveConfig);
    }

    @PostMapping("/delete.json")
    @Function("crm.crmRepresentiveConfig.del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        crmRepresentiveConfigService.batchDelCrmRepresentiveConfig(idList);
        return JsonResult.success();
    }
    

}
